package com.qk.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qk.common.PageResult;
import com.qk.dto.UserDto;
import com.qk.entity.User;
import com.qk.exception.BusinessException;
import com.qk.mapper.UserMapper;
import com.qk.service.UserService;
import com.qk.utils.JwtUtils;
import com.qk.vo.LoginResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: UserServiceImpl
 * Package: com.qk.service.impl
 * Description:
 *
 * @Author zt
 * @Create 2025/7/28 17:11
 * @Version 1.0
 */
@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public PageResult<User> listUsers(UserDto userDto) {
        //1.设置分页参数
        PageHelper.startPage(userDto.getPage(), userDto.getPageSize());

        //2.调用 mapper接口查询
        List<User> userList =  userMapper.selectByCondition(userDto);

        //3.解析并封装结果
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        return new PageResult<>(pageInfo.getTotal(), pageInfo.getList());
    }

    /*
    新增用户
     */

    @Override
    public void add(User user) {
        //基础属性
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());

        //设置默认密码
        String password = user.getUsername() + "123";
        user.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));

        //调用 mapper接口，添加用户
        userMapper.add(user);
    }

    @Override
    public void delete(List<Integer> ids) {
        userMapper.delete(ids);
    }

    @Override
    public User getById(Integer id) {
        return userMapper.getById(id);
    }

    @Override
    public void update(User user) {
        //补充基础属性
        user.setUpdateTime(LocalDateTime.now());
        userMapper.update(user);
    }

    @Override
    public List<User> list() {
        return userMapper.list();
    }

    @Override
    public List<User> listByRole(String roleLabel) {
        return userMapper.listByRole(roleLabel);
    }

    @Override
    public List<User> listByDept(Integer deptId) {
        return userMapper.listByDept(deptId);
    }

    /*
    登录
     */

    @Override
    public LoginResultVo login(String username, String password) {
        //写登录的逻辑，根据用户名查询
        User user = userMapper.selectByUsername(username);

        //用户不存在
        if (user == null) {
            log.info("用户不存在");
            throw new BusinessException("用户或密码错误");
        }

        //密码错误
        if (!user.getPassword().equals(DigestUtils.md5DigestAsHex(password.getBytes()))) {
            log.info("密码错误");
            throw new BusinessException("用户或密码错误");
        }

        //登录状态status=0,停用
        if (user.getStatus() == 0) {
            log.info("用户已停用");
            throw new BusinessException("用户已停用");
        }

        //登录成功，封装结果
        LoginResultVo loginResultVo = new LoginResultVo();
        loginResultVo.setId(user.getId());
        loginResultVo.setUsername(user.getUsername());
        loginResultVo.setName(user.getName());
        loginResultVo.setImage(user.getImage());
        loginResultVo.setRoleLabel(user.getRoleLabel());

        //调用工具类，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put("id", user.getId());
        claims.put("username", user.getUsername());
        String jwt = JwtUtils.generateToken(claims);

        loginResultVo.setToken(jwt);

        return loginResultVo;
    }
}
